package com.cetccloud.ap.config;

import com.cetccloud.ap.controller.model.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.support.WebExchangeBindException;

/**
 * @ClassName ExceptionConfig
 * @Description TODO
 * @Author superz
 * @Date 2021/4/19 16:00
 * @Version 1.0
 **/
@Slf4j
@ControllerAdvice(annotations = RestController.class)
public class ExceptionConfig {

    @ExceptionHandler
    @ResponseBody
    public ApiResult handle(WebExchangeBindException ex) {
        log.error("处理参数异常: ", ex);
        return ApiResult.error(ex.getAllErrors().get(0).getDefaultMessage());
    }

    @ExceptionHandler
    @ResponseBody
    public ApiResult handle(Exception ex) {
        log.error("处理业务异常: ", ex);
        return ApiResult.error(ex.getMessage());
    }

    /*@ExceptionHandler
    public ResponseEntity<String> handle(HandlerMethod handlerMethod, WebExchangeBindException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getAllErrors().get(0).getDefaultMessage());
    }*/
}
